public class Solution67 {
    public String addBinary(String a, String b) {
        a = new StringBuilder(a).reverse().toString();
        b = new StringBuilder(b).reverse().toString();
        int carry = 0;
        StringBuilder res = new StringBuilder();
        int i;
        for (i = 0; i < a.length() || i < b.length(); i++) {
            int ai = i >= a.length() ? 0 : a.charAt(i) - '0';
            int bi = i >= b.length() ? 0 : b.charAt(i) - '0';
            int t = ai + bi + carry;
            carry = 0;
            if (t > 1) {
                t -= 2;
                carry = 1;
            }
            res.append(t);
        }
        if (carry == 1) {
            res.append(1);
        }
        return res.reverse().toString();
    }
}
